package com.aaa.power.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.dao.UserDao;
import com.aaa.power.entity.User;
import com.aaa.power.service.UserRoleService;
import com.aaa.power.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 用户信息表(User)表服务实现类
 *
 * @author makejava
 * @since 2020-11-13 09:33:15
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Autowired
    private UserRoleService userRoleService;

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer userId) {
        return this.userDao.queryById(userId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<User> queryAllByLimit(int offset, int limit) {
        return this.userDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增或修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public Result insertOrUpdate(User user) {
        int i = 0;
        if (user.getUserId() != null && user.getUserId() !=0) {
            user.setUpdateTime(new Date());
            i = userDao.update(user);
            if (i>0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"修改成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"修改失败");
            }
        }else {
            user.setCreateTime(new Date());
            user.setStatus("1");
            i = userDao.insert(user);
            if (i >0){
                return new Result(ReturnCode.SUCCESSS.getCode(),
                        ReturnCode.SUCCESSS.getMsg(),"添加成功");
            }else {
                return new Result(ReturnCode.FAIL.getCode(),
                        ReturnCode.FAIL.getMsg(),"添加失败");
            }
        }
    }


    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(Integer userId) {
        this.userRoleService.deleteById(userId);
        this.userDao.deleteById(userId);
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),true);
    }

    @Override
    public Result queryPageByParam(Integer pageNo, Integer pageSize, User user) {
        if(pageNo==null||pageSize==null){
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        //分页
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<User> pageInfo = new PageInfo<User>(userDao.queryAll(user));
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),pageInfo);
    }

    @Override
    public Result update(User user) {
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),this.userDao.update(user));
    }


    /**
     * 通过实体作为筛选条件查询
     *
     * @param userName 实例对象
     * @return 对象列表
     */
    @Override
    public User queryUserByParam(String userName) {
        User user = new User();
        //设置参数
        user.setUserName(userName);
        System.out.println(userName);
        //查询
        List<User> users = userDao.queryAll(user);
        //判断不空
        if(users != null&&users.size() > 0){
            return users.get(0);
        }
        return null;
    }
}